exports.questionDetail = function (req, res) {
//问题内容页
    //获取restful参数
    var restfulParams = req.routeInfo.restfulParams;
    //最终结果
    var resultData = {};
    var questionData = 0;
    var masterInfo = 0;

    //任务流创建
    async.waterfall([
        //获取问题
        function (cb) {

            R.SEND_HTTP(
                req,
                {
                    url: CORE_APIConfig.coreAPI.search_content + "/fc_id/" + restfulParams.fc_id,
                    method: 'POST',
                    data: {
                        fields: [
                            "fc_id",
                            "fc_uid",
                            "fc_title",
                            "fc_content",
                            "fc_images",
                            "fc_answer_count",
                            "fc_collect_count",
                            "fc_create_time",
                            "fc_share_count",
                            "fc_zan_count",
                            "u_id",
                            "u_nickname",
                            "u_header_url",
                            "fc_browse_count"
                        ].join(",")
                    }

                },
                function (err, data) {
                    if (err) {
                        return RES.response(res, false, data);
                    }


                    var questionInfo = data.data;

                    if (questionInfo.length > 0) {

                        resultData.questionData = data.data;

                        questionData = questionInfo;

                        cb();

                    } else {
                        R.SEND_HTTP(
                            req,
                            {
                                url: CORE_APIConfig.coreAPI.search_question + "/fc_id/" + restfulParams.fc_id,
                                method: 'POST',
                                data: {
                                    fields: [
                                        "fc_id",
                                        "fc_uid",
                                        "fc_title",
                                        "fc_content",
                                        "fc_images",
                                        "fc_answer_count",
                                        "fc_collect_count",
                                        "fc_create_time",
                                        "fc_share_count",
                                        "fc_zan_count",
                                        "fc_browse_count"
                                    ].join(",")
                                }

                            },
                            function (err, data) {
                                if (err) {
                                    return RES.response(res, false, data);
                                }

                                resultData.questionData = data.data;
                                questionData = data.data;
                                cb();
                            }
                        );
                    }
                    cb();
                }
            );
        },

        function (cb) {
            if (restfulParams.u_type == 1) {
                var u_id = restfulParams.o_uid;
            } else {
                var u_id = restfulParams.u_id;
            }
            if (u_id) {
                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.search_answer_userinfo + "/fcc_fcid/" + restfulParams.fc_id + "/fca_uid/" + u_id,
                        method: 'POST',
                        data: {
                            fields: [
                                "fca_id"
                            ].join(",")
                        }
                    },
                    function (err, data) {
                        if (err) {
                            return RES.response(res, false, data);
                        }

                        var answerInfo = data.data;
                        if (answerInfo.length > 0) {
                            resultData.hasAnswered = 1;
                        } else {
                            resultData.hasAnswered = 0;
                        }
                        cb();
                    }
                );
            } else {

                cb()
            }
        },
        //是否关注
        function (cb) {

            R.SEND_HTTP(
                req,
                {
                    url: CORE_APIConfig.coreAPI.searchQuestionConcern + "/is_delete/0/fqc_fcid/" + restfulParams.fc_id + "/fqc_uid/" + restfulParams.u_id,
                    method: 'POST',
                    data: {
                        fields: ["fqc_id"].join(",")
                    }

                },
                function (err, data) {
                    if (err) {
                        return RES.response(res, false, data);
                    }
                    var is_concern = data.data;

                    if (is_concern.length <= 0) {
                        resultData.isConcernData = 0;
                    } else {
                        resultData.isConcernData = 1;
                    }

                    cb();
                }
            );
        },
        //浏览数加1
        function (cb) {
            if (questionData.length > 0) {
                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.update_fc_browse_count,
                        method: 'POST',
                        data: {
                            fc_id: restfulParams.fc_id,
                            fc_browse_count: parseInt(questionData[0].fc_browse_count) + 1
                        }

                    },

                    function (err, data) {
                        console.log(data);
                        if (err) {
                            return RES.response(res, false, data);
                        }
                        resultData.isaddone = data;
                        cb();
                    }
                );
            } else {
                cb()
            }

        },
        //查询达人信息
        function (cb) {
            if (questionData.length > 0) {
                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.search_forum_master + "/m_uid/" + resultData.questionData[0]['fc_uid'],
                        method: 'POST',
                        data: {
                            fields: [
                                "m_browse_count",
                            ].join(",")
                        }
                    },
                    function (err, data) {
                        if (err) {
                            return RES.response(res, false, err);
                        }
                        masterInfo = data.data;
                        if (masterInfo.length > 0) {
                            resultData.m_browse_count = masterInfo[0].m_browse_count;
                            cb();
                        } else {
                            cb();
                        }
                    }
                );
            } else {
                cb()
            }

        },
        //更新达人的浏览量
        function (cb) {
            if (masterInfo > 0) {
                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.update_master,
                        method: 'POST',
                        data: {
                            m_uid: resultData.questionData[0].fc_uid,
                            m_browse_count: parseInt(m_browse_count) + 1
                        }

                    },

                    function (err, data) {
                        //console.log(data)
                        if (err) {
                            return RES.response(res, false, data);
                            cb();
                        }
                        resultData.isMasterAddone = data;
                        cb();
                    }
                );
            } else {
                cb();
            }
        },

        /*
        function (cb) {
            if (resultData.answerData.length !== 0) {
                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.search_answer_comment_userinfo + "/fcac_is_reply/0/fcac_fcaid/" + resultData.answerData[0].fca_id,
                        method: 'POST',
                        data: {
                            fields: [
                                "fcac_id as fcc_id",
                                "fcac_content as fcc_content",
                                "u_nickname",
                                "u_header_url",
                                "fcac_zan_count as fcc_zan_count",
                                "fcac_uid as fcc_uid",
                                "fcac_is_reply as fcc_is_reply",
                                "fcac_create_time as fcc_create_time",
                            ].join(",")
                        }
                    },
                    function (err, data) {
                        //console.log(data);
                        //判断是否有错误信息
                        if (err) {
                            return RES.response(res, false, data);
                        }
                        resultData.answerCommentData = data.data;
                        cb();
                    }
                );
            }else{
                cb();
            }
        },
        function (cb) {

            //创建添加任务
            var tasks = [];

            //console.log(resultData.answerCommentData)

            if(resultData.answerCommentData != undefined && resultData.answerCommentData.length>0){

                //循环节点列表
                resultData.answerCommentData.forEach(function (item, i) {

                    //创建每一个插入任务
                    tasks.push(function (cb) {
                        if (item.fcc_is_reply == 0) {
                            R.SEND_HTTP(
                                req,
                                {
                                    url: CORE_APIConfig.coreAPI.search_answer_comment_userinfo + "/fcac_reply_uid/" + item.fcc_id+"/skip/0/limit/2/count/1",
                                    method: 'POST',
                                    data: {
                                        fields: ["fcac_content as fcc_content", "u_nickname", "fcac_create_time as fcc_create_time"].join(",")
                                    }
                                },
                                function (err, data) {

                                    //console.log(data);
                                    item.replyData = data.data;
                                    item.replyCount = data.count;
                                    cb();
                                }
                            );
                        } else {
                            cb();
                        }
                    });
                });

            }else{

                cb();
            }

            
            async.waterfall(tasks, cb);
        },
        */
    ], function (err, result) {

        //返回最终拼接结果
        RES.response(res, true, resultData);

    });

}